<template>
  <div class="JobDetails">
    <NavBar
      fixed
      placeholder
      :title="info.icoNo"
      z-index="999"
      @click-right="onClickRight"
      ref="nav"
    >
      <template #left>
        <Icon
          name="arrow-left"
          size="0.53rem"
          color="#000"
          @click="$router.push('JobManagement')"
        />
      </template>
      <template #right>
        <!-- <span style="color: #000" @click="toJump"> 查看记录</span> -->
        <!-- <img
          class="right-img"
          src="../../../assets/img/history.png"
          @click="toJump"
          alt=""
        /> -->
        <Icon name="notes-o" size="0.53rem" color="#000" @click="toJump" />
      </template>
    </NavBar>
    <div class="main">
      <Field
        v-if="'title' in info"
        v-model="info.title"
        label="标题"
        placeholder="请输入标题"
        input-align="right"
        required
      ></Field>
      <Field
        v-if="'uegencyDegree' in info"
        v-model="info.uegencyDegree1"
        label="紧急程度"
        placeholder="请选择紧急程度"
        input-align="right"
        is-link
        readonly
        required
        @click="showLV"
      ></Field>
      <Field
        v-if="'level' in info"
        v-model="info.level1"
        label="作业级别"
        placeholder="请选择作业级别"
        input-align="right"
        is-link
        readonly
        required
        @click="showLv2"
      ></Field>
      <Field
        v-if="'respon' in info"
        v-model="info.respon"
        label="作业负责人"
        placeholder="请选择作业负责人"
        input-align="right"
        required
      ></Field>
      <Field
        v-if="'tel' in info"
        v-model="info.tel"
        label="联系方式"
        placeholder="请输入联系方式"
        input-align="right"
        required
      ></Field>
      <Field
        v-if="'load' in info"
        v-model="info.load"
        label="所需负荷"
        type="digit"
        placeholder="请输入所需负荷(KW以内)"
        input-align="right"
        required
      ></Field>
      <Field
        v-if="'startDtm' in info"
        v-model="info.startDtm"
        label="作业开始时间"
        placeholder="请选择作业开始时间"
        input-align="right"
        is-link
        readonly
        required
        @click="setStartTime"
      ></Field>
      <Field
        v-if="'endDtm' in info"
        v-model="info.endDtm"
        label="作业结束时间"
        placeholder="请选择作业结束时间"
        input-align="right"
        is-link
        readonly
        required
        @click="setEndTime"
      ></Field>
      <Field
        v-if="'jobSite' in info"
        v-model="info.jobSite"
        label="作业部位"
        placeholder="请输入作业部位"
        input-align="right"
        required
      ></Field>
      <Field
        v-if="'otherWork' in info"
        v-model="info.otherWork"
        label="涉及的其他危险作业"
        placeholder="请输入涉及的其他危险作业"
        input-align="right"
        required
      ></Field>
      <Field
        v-if="'reason' in info"
        v-model="info.reason"
        label="断路原因"
        placeholder="请输入断路原因"
        input-align="right"
        required
      ></Field>

      <div class="t" v-if="'useArea' in info">使用区域</div>
      <textarea
        v-if="'useArea' in info"
        v-model="info.useArea"
        placeholder="请输入使用区域*"
        cols="30"
        rows="10"
        required
      ></textarea>

      <Field
        v-if="'draft' in info"
        label="请上传断路草图"
        required
        label-width="8em"
        readonly
        :border="false"
      />
      <Uploader
        v-if="'draft' in info"
        v-model="fileList"
        multiple
        :max-count="3"
        capture="camera"
      />

      <Field
        v-if="'workArea' in info"
        v-model="info.workArea1"
        label="作业区域"
        placeholder="请选择作业区域"
        input-align="right"
        is-link
        readonly
        required
        @click="showArea"
      ></Field>
      <Field
        v-if="'workPosition' in info"
        v-model="info.workPosition"
        label="作业位置"
        input-align="right"
        required
      ></Field>
      <Field
        v-if="'workPosition' in info"
        v-model="info.workCoord"
        label="作业坐标"
        placeholder="无锡拈花湾景区"
        input-align="right"
        is-link
        readonly
        required
        @click="getArea"
      ></Field>

      <Field
        v-if="'content' in info"
        label="作业内容"
        readonly
        required
      ></Field>
      <textarea
        v-if="'content' in info"
        v-model="info.content"
        placeholder="请输入作业内容"
        cols="30"
        rows="10"
      ></textarea>
    </div>
    <div class="btnFa">
      <Button class="btn" @click="onSumbit">下一步</Button>
    </div>

    <!-- 级别 -->
    <Popup v-model="show" position="bottom" :style="{ height: '50%' }">
      <Picker
        title="紧急程度"
        show-toolbar
        :columns="columns1"
        @confirm="onConfirm1"
        @cancel="show = false"
        value-key="name"
      />
    </Popup>
    <Popup v-model="show1" position="bottom" :style="{ height: '50%' }">
      <Picker
        title="作业级别"
        show-toolbar
        :columns="columns"
        @confirm="onConfirm"
        @cancel="show1 = false"
        value-key="name"
      />
    </Popup>
    <Popup v-model="show4" position="bottom" :style="{ height: '50%' }">
      <Picker
        title="作业区域"
        show-toolbar
        :columns="columns2"
        @confirm="onConfirm4"
        @cancel="show4 = false"
        value-key="name"
      />
    </Popup>
    <Popup
      class="time"
      v-model="show2"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <DatetimePicker
        v-model="currentDate"
        type="datetime"
        title="作业开始时间"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="startConfirm"
        @cancel="show2 = false"
        item-height="1rem"
      ></DatetimePicker>
    </Popup>
    <Popup
      class="time"
      v-model="show3"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <DatetimePicker
        v-model="currentDate"
        type="datetime"
        title="作业结束时间"
        :min-date="minDate1"
        :max-date="endMaxDate"
        @confirm="endConfirm"
        @cancel="show3 = false"
        item-height="1rem"
      ></DatetimePicker>
    </Popup>
    <Popup v-model="isShowMap" position="bottom" :style="{ height: '100%' }">
      <AMap @cancel="closeMap" @confirm="getPoint" />
    </Popup>
  </div>
</template>

<script>
import uploadImages from "@/utils/uploadImages1.js";
import AMap from "components/AMap";
import { getHomeWorkInfo } from "@/utils/homeworkJudge";
import {
  WhenHotList,
  EquipmentRepairServicePost,
  JobLevelList,
} from "@/api/work.js";
import Layout from "components/Layout";
import {
  Popup,
  Picker,
  NavBar,
  Icon,
  Field,
  Cell,
  Uploader,
  Button,
  DatetimePicker,
  Toast,
} from "vant";
export default {
  name: "JobDetails",

  components: {
    Popup,
    Picker,
    Uploader,
    Layout,
    NavBar,
    Icon,
    Field,
    Cell,
    Button,
    DatetimePicker,
    Toast,
    AMap,
  },

  directives: {},

  data() {
    return {
      uegencyDegree: "",
      level: "",
      address: "",
      point: {},
      workArea: "",
      isShowMap: false,
      fileList: [],
      show: false,
      show1: false,
      columns2: [],
      columns1: [],
      columns: [],
      show2: false,
      show3: false,
      show4: false,
      minDate: new Date(),
      minDate1: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      maxDate1: new Date(2025, 10, 1),
      currentDate: new Date(2021, 0, 17),
      LvChangedFlag: false, //判断是否选择级别
      timeDifference: {
        timeRange: 0,
        timeUnit: null,
      }, //时间差数据
      icoNo: "",
      info: {
        // title: "",
        uegencyDegree: "",
        level: "",
        respon: "",
        startDtm: "",
        endDtm: "",
        workArea: "",
        workPosition: "无锡拈花湾景区",
        workCoord: "120.068788,31.417695 ", //坐标
        content: "",
      },
    };
  },
  computed: {
    endMaxDate() {
      if (this.timeDifference.timeUnit == null) return new Date();
      if (this.timeDifference.timeUnit == "day") {
        return new Date(
          this.minDate1.valueOf() +
            this.timeDifference.timeRange * 24 * 60 * 60 * 1000
        );
      }
      if (this.timeDifference.timeUnit == "hours") {
        return new Date(
          this.minDate1.valueOf() +
            this.timeDifference.timeRange * 60 * 60 * 1000
        );
      }

      return new Date();
    },
  },
  mounted() {
    console.log(this.$route.query);
    // this.getMenuLv();
    // this.getMenuLv2();
    // this.getMenuArea();

    if ("uegencyDegree" in this.info) {
      this.info.uegencyDegree1 = "";
    }
    if ("level" in this.info) {
      this.info.level1 = "";
    }
    if ("workArea" in this.info) {
      this.info.workArea1 = "";
    }

    // if (this.$route.query.info) {
    //   let info = JSON.parse(this.$route.query.info);
    //   this.info = info;
    // } else {
    //   const info = getHomeWorkInfo(this.$route.query.dgwkName);
    //   this.info = info;
    //   this.info.dgwkName = this.$route.query.dgwkName;
    // }
    // this.info.icoNo = this.$route.query.icoNo;
    const info = getHomeWorkInfo(this.$route.query.dgwkName);
    this.info = { ...info, ...this.$route.query };
    this.getMenuLv3();
  },

  methods: {
    showLV() {
      //紧急程度
      this.getMenuLv();
      this.show = true;
    },
    showLv2() {
      //作业级别
      this.getMenuLv2();
      // this.show1 = true;
    },
    showArea() {
      //作业区域
      this.getMenuArea();
      this.show4 = true;
    },
    async getMenuLv() {
      //紧急程度
      const data = await EquipmentRepairServicePost({
        code: "DgwkUrgencyDegree",
      });
      // console.log("222222", data.param.data);
      this.columns1 = data.param.data;
    },
    async getMenuLv2() {
      //作业级别
      const data = await JobLevelList({
        dgwkName: this.info.dgwkName,
      });
      console.log("222222", data.param.data);
      this.columns = data.param.data;
      if (!("level" in this.info)) {
        // console.log("没有级别");
        this.timeDifference = data.param.data[0];
      }
      this.show1 = true;
    },
    async getMenuLv3() {
      //作业级别
      const data = await JobLevelList({
        dgwkName: this.info.dgwkName,
      });
      // console.log("没有级别");
      console.log(123, data);
      this.timeDifference = data.param.data[0];
    },
    async getMenuArea() {
      //作业区域
      const data = await EquipmentRepairServicePost({
        code: "DgwkWorkArea",
      });
      // console.log("222222", data.param.data);
      this.columns2 = data.param.data;
    },

    //扫一扫事件
    toJump() {
      console.log(1);
      this.$router.push({
        path: "WorkRecord",
        query: this.$route.query,
      });
    },
    onClickLeft() {},
    onClickRight() {},
    toRecordsList() {
      this.$router.push({
        path: "RecordsList",
        query: {},
      });
    },
    async onSumbit() {
      if ("title" in this.info && this.info.title.trim().length <= 0)
        return Toast.fail("请输入标题");
      if (
        "uegencyDegree" in this.info &&
        this.info.uegencyDegree.trim().length <= 0
      )
        return Toast.fail("请选择紧急程度");
      if ("level" in this.info && this.info.level.trim().length <= 0)
        return Toast.fail("请选择作业级别");
      if ("respon" in this.info && this.info.respon.trim().length <= 0)
        return Toast.fail("请选择作业负责人");
      if ("tel" in this.info && this.info.tel.trim().length <= 0)
        return Toast.fail("请输入联系方式");
      if ("load" in this.info && this.info.load.trim().length <= 0)
        return Toast.fail("请输入所需负荷(KW以内)");
      if ("startDtm" in this.info && this.info.startDtm.trim().length <= 0)
        return Toast.fail("请选择作业开始时间");
      if ("endDtm" in this.info && this.info.endDtm.trim().length <= 0)
        return Toast.fail("请选择作业结束时间");
      if ("jobSite" in this.info && this.info.jobSite.trim().length <= 0)
        return Toast.fail("请输入作业部位");
      if ("otherWork" in this.info && this.info.otherWork.trim().length <= 0)
        return Toast.fail("请输入涉及的其他危险作业");
      if ("reason" in this.info && this.info.reason.trim().length <= 0)
        return Toast.fail("请输入断路原因");
      if ("useArea" in this.info && this.info.useArea.trim().length <= 0)
        return Toast.fail("请输入使用区域");
      if ("draft" in this.info && this.info.draft.trim().length <= 0)
        return Toast.fail("请上传断路草图");
      if ("workArea" in this.info && this.info.workArea.trim().length <= 0)
        return Toast.fail("请选择作业区域");
      // if (
      //   "workPosition" in this.info &&
      //   this.info.workPosition.trim().length <= 0
      // )
      //   return Toast.fail("请选择作业位置");
      if ("content" in this.info && this.info.content.trim().length <= 0)
        return Toast.fail("请输入作业内容");

      if ("draft" in this.info) {
        // 上传图片和附件接口
        let pic = await uploadImages(this.fileList);
        this.info.draft = pic.join(",");
      }
      console.log(this.info);
      let query = this.info;

      // return console.log(JSON.parse(query.info));
      this.$router.push({
        path: "JobDetailsNext",
        query,
      });
    },

    onConfirm1(value, index) {
      // 紧急程度
      console.log(value, index);
      this.info.uegencyDegree = value.id;
      this.info.uegencyDegree1 = value.name;

      this.show = false;
    },
    onConfirm(value, index) {
      // 作业级别
      console.log(value, index);
      this.info.level = value.id;
      this.info.level1 = value.name;
      this.timeDifference = {
        timeRange: value.timeRange,
        timeUnit: value.timeUnit,
      };
      this.show1 = false;
    },
    onConfirm4(value, index) {
      // 作业区域
      console.log(value, index);
      this.info.workArea = value.id;
      this.info.workArea1 = value.name;
      this.show4 = false;
    },
    onCancel() {},
    setStartTime() {
      if ("level" in this.info && this.info.level.length <= 0) {
        return Toast.fail("请先选择级别");
      }
      this.show2 = true;
    },
    setEndTime() {
      if (this.info.startDtm.length <= 0) {
        return Toast.fail("请先选择级别开始时间");
      }
      console.log(this.endMaxDate);
      this.show3 = true;
    },
    startConfirm(value) {
      console.log(this.getTime(value));
      this.info.startDtm = this.getTime(value);
      this.minDate1 = value;
      this.show2 = false;
    },
    endConfirm(value) {
      console.log(this.getTime(value));
      this.info.endDtm = this.getTime(value);
      this.maxDate = value;
      this.show3 = false;
    },
    getTime(time) {
      const year = time.getFullYear();
      const Month = time.getMonth() + 1;
      const Date = time.getDate();
      let hour = time.getHours();
      hour = hour < 10 ? "0" + hour : hour;
      let m = time.getMinutes();
      m = m < 10 ? "0" + m : m;
      return `${year}-${Month}-${Date} ${hour}:${m}`;
    },
    //弹出地图
    getArea() {
      return;
      this.isShowMap = true;
      // let query = {
      //   ...this.$route.query,
      //   ...this.info,
      // };
      // console.log(this.$route.query, this.info);
      // // return console.log(query);
      // this.$router.push({
      //   path: "changeArea",
      //   query: { info: JSON.stringify(query) },
      // });
    },
    //获取地址坐标
    getPoint({ lat, lng, address }) {
      this.closeMap();
      this.info.workCoord = `${lng},${lat}`;
      this.info.workPosition = address;
    },
    //关闭地图
    closeMap() {
      this.isShowMap = false;
    },
  },
};
</script>

<style lang="less" scoped>
@import "~@/style/var.less";
.JobDetails {
  .flex {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  background-color: #f7f7f9;
  padding: 30px 0;
  // /deep/.van-nav-bar__content {
  //   background-color: #3982f6;

  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  // }
  .main {
    padding-bottom: 150px;
    background-color: #fff;
    .search-icon {
      color: #3982f6;
      margin-top: 8px;
      margin-left: 8px;
    }
    .t {
      font-size: 28px;
      margin: 10px;
      margin-left: 28px;
    }
    textarea {
      width: 692px;
      height: 157px;
      margin: 10px 0;
      margin-left: 50%;
      padding: 15px;
      transform: translate(-50%);
      border: 1px solid #dddddd;
      font-size: 26px;
      border-radius: 6px;
    }
    /deep/.van-cell__title span {
      color: black;
    }
    .location-box {
      width: 100%;
      display: flex;
      align-items: center;
      div {
        font-size: 28px;
        color: #999999;
      }
    }
    .van-uploader {
      margin-left: 20px;
    }
  }
  .btnFa {
    width: 100%;
    position: fixed;
    bottom: 0px;
    left: 50%;
    transform: translate(-50%);
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    padding-bottom: 10px;
  }
  .btn {
    height: 98px;
    width: 90%;
    background: #fff;
    border-radius: 6px;
    font-weight: 500;
    color: @primary;
    border: 1px solid @primary;
    font-size: 36px;
  }
  .time {
    /deep/.van-picker {
      height: 100%;
      .van-picker__columns {
        margin-top: 60px;
      }
    }
  }
  .right-img {
    width: 0.53rem;
  }
  /deep/.van-uploader__upload {
    border: 2px dashed #e16712;
    width: 110px;
    height: 110px;
    border-radius: 4px;
    background: #fff;
    .van-icon {
      color: rgb(225, 103, 18);
      &::before {
        content: "\F0A2";
      }
    }
  }
}
</style>